package com.zhisuo.apicenter.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zhisuo.apicenter.model.DeliveryOrder;
import com.zhisuo.apicenter.model.SendOrder;
import com.zhisuo.apicenter.model.ZhiSuoCar;
import com.zhisuo.apicenter.model.ZhiSuoCarBox;

import java.util.List;

/**
 * created by Intellij IDEA
 *
 * @author Chengzhifeng
 * @date 2020/10/13
 * @statement
 */
public class TcpUtil {

    /**
     * 单个开门指令
     *
     * @return
     */
    public static String openDoor(ZhiSuoCar car, DeliveryOrder order, List<ZhiSuoCarBox> listZhiSuoCarBox,String TCPURL) {

            JSONObject openboxjson=new JSONObject();
            openboxjson.put("name","dooropen");
            openboxjson.put("vin",car.getSn());
            JSONArray boxarray=new JSONArray();

            JSONObject boxjson=null;
            for(ZhiSuoCarBox carbox:listZhiSuoCarBox){
                boxjson=new JSONObject();
                if(order.getBoxcode().equals(carbox.getName())){

                    boxjson.put("name",carbox.getName());
                    boxjson.put("value","1");

                }else{
                    boxjson.put("name",carbox.getName());
                    boxjson.put("value","0");
                }
                boxarray.add(boxjson);
            }
            openboxjson.put("boxdoor",boxarray);
            //System.out.println("openboxjson------"+ openboxjson.toString());
            String text = HttpclientUtils.postJson(TCPURL, openboxjson.toString());
            //System.out.println("openboxjson----text--"+ text);
            //测试环境打开
            text="{errorCode:0}";
            return text;


    }




    /**
     * 多个开门指令
     *
     * @return
     */
    public static String openSendOrderManyDoor(ZhiSuoCar car, List<SendOrder> listorder, List<ZhiSuoCarBox> listZhiSuoCarBox,String TCPURL) {

        JSONObject openboxjson=new JSONObject();
        openboxjson.put("name","dooropen");
        openboxjson.put("vin",car.getSn());
        JSONArray boxarray=new JSONArray();

        JSONObject boxjson=null;
        for(ZhiSuoCarBox carbox:listZhiSuoCarBox){
            boxjson=new JSONObject();
            for(SendOrder order:listorder){
                if(order.getBoxcode().equals(carbox.getName())){
                    boxjson.put("name",carbox.getName());
                    boxjson.put("value","1");

                }else{
                    boxjson.put("name",carbox.getName());
                    boxjson.put("value","0");
                }
            }

            boxarray.add(boxjson);
        }
        openboxjson.put("boxdoor",boxarray);
        //System.out.println("openboxjson------"+ openboxjson.toString());
        String text = HttpclientUtils.postJson(TCPURL, openboxjson.toString());
        //System.out.println("openboxjson----text--"+ text);
        text="{errorCode:0}";
        return text;


    }


    /**
     * 多个开门指令
     *
     * @return
     */
    public static String openManyDoor(ZhiSuoCar car, List<DeliveryOrder> listorder, List<ZhiSuoCarBox> listZhiSuoCarBox,String TCPURL) {

        JSONObject openboxjson=new JSONObject();
        openboxjson.put("name","dooropen");
        openboxjson.put("vin",car.getSn());
        JSONArray boxarray=new JSONArray();

        JSONObject boxjson=null;
        JSONObject oneboxjson=null;

        JSONArray opendoorarray=new JSONArray();
        for(ZhiSuoCarBox carbox:listZhiSuoCarBox){


            boxjson=new JSONObject();
            oneboxjson=new JSONObject();
            Boolean flag=false;
            for(DeliveryOrder order:listorder){
                if(order.getBoxcode().equals(carbox.getName())){
                    flag=true;
                    break;
                }else{
                    flag=false;
                }
            }



            if(flag){
                boxjson.put("name",carbox.getName());
                boxjson.put("value","1");
                oneboxjson.put("code",carbox.getName());
                oneboxjson.put("isopen","1");
                oneboxjson.put("pos",carbox.getPos());
            }else{
                boxjson.put("name",carbox.getName());
                boxjson.put("value","0");
                oneboxjson.put("code",carbox.getName());
                oneboxjson.put("isopen","0");
                oneboxjson.put("pos",carbox.getPos());
            }
            boxarray.add(boxjson);
            opendoorarray.add(oneboxjson);
        }
        openboxjson.put("boxdoor",boxarray);
        //System.out.println("openboxjson------"+ openboxjson.toString());
        String text = HttpclientUtils.postJson(TCPURL, openboxjson.toString());
        //System.out.println("openboxjson----text--"+ text);
        text="{errorCode:0}";
        return text;


    }

    /**
     * 开门指令
     *
     * @return
     */
    public static String openSendDoor(ZhiSuoCar car, SendOrder order, List<ZhiSuoCarBox> listZhiSuoCarBox,String TCPURL) {

        JSONObject openboxjson=new JSONObject();
        openboxjson.put("name","dooropen");
        openboxjson.put("vin",car.getSn());
        JSONArray boxarray=new JSONArray();

        JSONObject boxjson=null;
        for(ZhiSuoCarBox carbox:listZhiSuoCarBox){
            boxjson=new JSONObject();
            if(order.getBoxcode().equals(carbox.getName())){

                boxjson.put("name",carbox.getName());
                boxjson.put("value","1");

            }else{
                boxjson.put("name",carbox.getName());
                boxjson.put("value","0");
            }
            boxarray.add(boxjson);
        }
        openboxjson.put("boxdoor",boxarray);
        //System.out.println("openboxjson------"+ openboxjson.toString());
        String text = HttpclientUtils.postJson(TCPURL, openboxjson.toString());
        text="{errorCode:0}";
        return text;


    }



}
